package org.ycsoft.metroid;


import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;


public class RepoMetroid extends Activity{
	
	public TextView title, txtCity, txtMean;
	public EditText edtCity;
	public Spinner spnMean;
	public Button repoValid, repoCancel;
	public static List<String> means;
	public String KEY_URL="url";
	
	
	@Override
	public void onCreate(Bundle icicle)
	{
		super.onCreate(icicle);
		setContentView(R.layout.search_repo);
		title=(TextView)findViewById(R.id.title_repo);
		txtCity=(TextView)findViewById(R.id.txtcity_repo);
		txtMean=(TextView)findViewById(R.id.txtmean_repo);
		edtCity=(EditText)findViewById(R.id.city_repo);
		spnMean=(Spinner)findViewById(R.id.mean_repo);
		repoValid=(Button)findViewById(R.id.ok_repo);
		repoCancel=(Button)findViewById(R.id.nop_repo);
		
		
		loadFields();

	}
	
	public void loadFields()
	{
		
		title.setText(R.string.repo_title);
		txtCity.setText(R.string.repo_city);
		txtMean.setText(R.string.repo_mean);
		repoValid.setText(R.string.valid);
		
		
		means=new ArrayList<String>();
		means.add("Bus");
		means.add("Metro");
		means.add("Both");
			
		ArrayAdapter<String> meanItems = new ArrayAdapter<String>(this,
				R.layout.list, means);
		spnMean.setAdapter(meanItems);
		
		repoValid.setOnClickListener(repoValidListener);
		repoCancel.setOnClickListener(repoCancelListener);
	}
	
	private OnClickListener repoValidListener = new OnClickListener(){
		public void onClick(View v){
			try {
				getRepoStream();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	};
	
	private OnClickListener repoCancelListener = new OnClickListener(){
		public void onClick(View v){
			
				finish();
			
		}
	};
	
	private void getRepoStream() throws IOException
	{
		/*String url=Commons.URLRepo+Commons.KEY_CITY+Commons.EQ+edtCity.getText()+
		Commons.KEY_MEAN+Commons.EQ+spnMean.getSelectedItem().toString();*/
		
			Intent i = new Intent(this, RepoSearchResults.class);
			//i.putExtra(KEY_URL, url);
			i.putExtra(Commons.KEY_CITY, edtCity.getText().toString());
			i.putExtra(Commons.KEY_MEAN, spnMean.getSelectedItem().toString());
			startActivity(i);
			//close activity
			//finish();
	}
	
}
	